package com.ruoyi.waterPrice.service.impl;

import java.math.BigDecimal;
import java.security.Security;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.base.datam.WiLandB;
import com.ruoyi.common.core.domain.business.waterPrice.*;
import com.ruoyi.common.core.enums.FeeKindEnum;
import com.ruoyi.common.core.enums.FeeModeTypeEnum;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.waterPrice.controller.WiOrgfeeYController;
import com.ruoyi.waterPrice.mapper.*;
import com.ruoyi.waterPrice.service.IWiOrgfeeYService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 水管机构水费核算Service业务层处理
 *
 * @author ps
 * @date 2023-02-16
 */
@Service
public class WiOrgfeeYServiceImpl implements IWiOrgfeeYService {

    private static final Logger log = LoggerFactory.getLogger(WiOrgfeeYServiceImpl.class);

    @Autowired
    private WiOrgfeeYMapper wiOrgfeeYMapper;
    @Autowired
    private WiOrgpriceYMapper wiOrgpriceYMapper;
    @Autowired
    private WiPriceYMapper wiPriceYMapper;
    @Autowired
    private WiFeemodeYMapper wiFeemodeYMapper;
    @Autowired
    private WiFarmfeeYMapper wiFarmfeeYMapper;

    /**
     * 查询水管机构水费核算
     *
     * @param feeYr 水管机构水费核算主键
     * @return 水管机构水费核算
     */
    @Override
    public WiOrgfeeY selectWiOrgfeeYByFeeYrAndOrgCd(String feeYr, String OrgCd) {
        return wiOrgfeeYMapper.selectWiOrgfeeYByFeeYrAndOrgCd(feeYr, OrgCd);
    }

    /**
     * 查询水管机构水费核算列表
     *
     * @param wiOrgfeeY 水管机构水费核算
     * @return 水管机构水费核算
     */
    @Override
    public List<WiOrgfeeY> selectWiOrgfeeYList(WiOrgfeeY wiOrgfeeY) {
        return wiOrgfeeYMapper.selectWiOrgfeeYList(wiOrgfeeY);
    }

    /**
     * 新增水管机构水费核算
     *
     * @param wiOrgfeeY 水管机构水费核算
     * @return 结果
     */
    @Override
    public int insertWiOrgfeeY(WiOrgfeeY wiOrgfeeY) {
        return wiOrgfeeYMapper.insertWiOrgfeeY(wiOrgfeeY);
    }

    /**
     * 修改水管机构水费核算
     *
     * @param wiOrgfeeY 水管机构水费核算
     * @return 结果
     */
    @Override
    public int updateWiOrgfeeY(WiOrgfeeY wiOrgfeeY) {
        return wiOrgfeeYMapper.updateWiOrgfeeY(wiOrgfeeY);
    }

    /**
     * 批量删除水管机构水费核算
     *
     * @param feeYrs 需要删除的水管机构水费核算主键
     * @return 结果
     */
    @Override
    public int deleteWiOrgfeeYByFeeYrs(String[] feeYrs) {
        return wiOrgfeeYMapper.deleteWiOrgfeeYByFeeYrs(feeYrs);
    }

    /**
     * 删除水管机构水费核算信息
     *
     * @param feeYr 水管机构水费核算主键
     * @return 结果
     */
    @Override
    public int deleteWiOrgfeeYByFeeYrAndOrgCd(String feeYr, String OrgCd) {
        return wiOrgfeeYMapper.deleteWiOrgfeeYByFeeYrAndOrgCd(feeYr, OrgCd);
    }

    /**
     * 水费核定
     * 地块性质 9未定 灌溉方式 其他4
     *
     * @param year
     * @param orgCd 说明：先查计收方式定义表，看以哪种方式计费
     *              然后再查机构差异化定价表，看是否存在 存在就核定（还要看地块类型和灌溉方式有没有，没有直接返回提示）
     *              不存在 再查年度执行水价表 若存在 根据地块类型和灌溉方式核定水费
     *              若不存在 直接用计收方式定义表的水价进行核定
     *              核定时还要考虑计划水量，超过计划水量就是超额用水 多出来的水量按超限水价计算
     */
    @Override
    public HashMap<String, Object> caleFee(String year, String orgCd) {
        HashMap<String, Object> res = new HashMap<>();
        /**查找执行水价 start*/
        BigDecimal areaPrice_wiFeemodeY;//年度水费计收方式定义亩均水价
        BigDecimal volPrice_wiFeemodeY;//年度水费计收方式定义方均水价
        BigDecimal overPrice_wiFeemodeY;//超限水价
        String endMonth;
        BigDecimal areaPrice_wiPriceY;//年度执行水价亩均水价
        BigDecimal volPrice_wiPriceY = new BigDecimal("0");//年度执行水价方均水价
        BigDecimal areaPrice_wiOrgpriceY;//机构差异化水价亩均水价
        BigDecimal volPrice_wiOrgpriceY = new BigDecimal("0");//机构差异化水价方均水价
        String modeType;//计收模式 0-按地块面积 1-按地块水量
        String feeKind;//收费方式 0-按农户 1-按机构（最小）
        int flag = 0;
        /**查年度水费计收方式定义表*/
        WiFeemodeY wiFeemodeY = wiFeemodeYMapper.selectWiFeemodeYByModeYr(year);
        if (wiFeemodeY == null) {
            res.put("status", false);
            res.put("msg", "计费模式中未查找到相关数据！");
            return res;
        } else {
            flag = 1;
            areaPrice_wiFeemodeY = wiFeemodeY.getAreaPrice();//年度水费计收方式亩均水价
            volPrice_wiFeemodeY = wiFeemodeY.getVolPrice();//年度水费计收方式方均水价
            overPrice_wiFeemodeY = wiFeemodeY.getOverPrice();//超限水价
            endMonth = wiFeemodeY.getEndMon();
            modeType = wiFeemodeY.getModeType();//计收模式 0-按地块面积 1-按地块水量
            feeKind = wiFeemodeY.getFeeKind();//收费方式 0-按农户 1-按机构（最小）
            /**查机构差异化水价定义表*/
            WiOrgpriceY wiOrgpriceY = wiOrgpriceYMapper.selectWiOrgpriceYByOrgCdAndYear(year, orgCd);
            if (wiOrgpriceY == null) {
                /**查年度执行水价定义表*/
                List<WiPriceY> wiPriceYList = wiPriceYMapper.selectWiPriceYByYear(year);
                if (wiPriceYList.size() == 0) {
                } else {
                    volPrice_wiPriceY = wiPriceYList.get(0).getVolPrice();
                    flag = 2;
                }
            } else {/**存在机构差异化水价定义*/
                volPrice_wiOrgpriceY = wiOrgpriceY.getVolPrice();
                flag = 3;
            }
        }
        /**查找执行水价 end*/

        log.info("执行水价标准 1-年度水费计收方式 2-年度执行水价 3-机构差异化水价 当前为：" + flag + " 计费模式 0-按地块面积 1-按地块水量 当前为： " + modeType + "收费方式 0-按农户 1-按机构（最小）当前为：" + feeKind);
        String startYm = BusinessUtils.getStartYm(year, endMonth);
        String endYm = year + endMonth;
        /**查找地块 并计算水费 start*/
        if (FeeKindEnum.ORG.getCode().equals(feeKind)) {//按机构
            //算水量 start 机构找断面 断面找测站 截止月份-前一年当前月+1 汇总水量和

            log.info("核定机构：" + orgCd + "=" + startYm + "=" + endYm);
            BigDecimal volSum = new BigDecimal(wiOrgpriceYMapper.getVolSum(orgCd, startYm, endYm) == null ? "0" : wiOrgpriceYMapper.getVolSum(orgCd, startYm, endYm));
            if (volSum.compareTo(new BigDecimal("0")) == 0) {
                res.put("status", false);
                res.put("msg", "该机构下不存在地块用水信息！");
                return res;
            }
            //算水量 end
            /**查限额水量*/
            BigDecimal planVolSum = new BigDecimal(wiOrgfeeYMapper.selectPlanVolSumOrgCd(orgCd, startYm, endYm) == null ? "0" : wiOrgfeeYMapper.selectPlanVolSumOrgCd(orgCd, startYm, endYm));
            //算地块面积 start
            BigDecimal wiLandBSizeSum = new BigDecimal("0");
            List<WiLandB> wiLandBList = wiOrgpriceYMapper.getWiLandBListByOrgCd(orgCd);
            for (WiLandB wiLandB : wiLandBList) {
                wiLandBSizeSum = wiLandBSizeSum.add(wiLandB.getLandSize());
            }
            if (wiLandBSizeSum.compareTo(new BigDecimal("0")) == 0) {
                res.put("status", false);
                res.put("msg", "该机构下不存在地块信息！");
                return res;
            }
            //算地块面积 end
            if (flag == 1) {
                int i = 0;
                BigDecimal waterFee;
                if (FeeModeTypeEnum.AREA.getCode().equals(modeType)) {//按地块面积 直接地块面积 * 亩均水价
                    i = insertWiOrgfeeY(year, orgCd, wiLandBSizeSum, volSum, areaPrice_wiFeemodeY, wiLandBSizeSum.multiply(areaPrice_wiFeemodeY));
                }
                if (FeeModeTypeEnum.VOL.getCode().equals(modeType)) {//按水量
                    if (volSum.compareTo(planVolSum) == 1) {//超限用水
                        waterFee = planVolSum.multiply(volPrice_wiFeemodeY).add((volSum.subtract(planVolSum)).multiply(overPrice_wiFeemodeY));
                    } else {
                        waterFee = volSum.multiply(volPrice_wiFeemodeY);
                    }
                    i = insertWiOrgfeeY(year, orgCd, wiLandBSizeSum, volSum, volPrice_wiFeemodeY, waterFee);
                }
                if (i < 1) {
                    res.put("status", false);
                    res.put("msg", "水费核定失败，请联系管理员！ errCode：002");
                    return res;
                }
            }
            if (flag == 2) {
                int i = 0;
                //定义水费
                BigDecimal waterFee = new BigDecimal("0");
                areaPrice_wiPriceY = new BigDecimal("0");
                if (FeeModeTypeEnum.AREA.getCode().equals(modeType)) {//按地块面积 直接地块面积 * 亩均水价
                    //遍历每个地块，根据土地性质和灌溉方式查询对应水价 查不到的，返回提示
                    for (WiLandB wiLandB : wiLandBList) {
                        WiPriceY wiPriceY = new WiPriceY();
                        wiPriceY.setPriceYr(year);
                        wiPriceY.setLandAttr(wiLandB.getLandAttr());
                        wiPriceY.setIrrMode(wiLandB.getIrrMode());
                        List<WiPriceY> wiPriceYS = wiPriceYMapper.selectWiPriceYList(wiPriceY);
                        if (wiPriceYS.size() == 0) {
                            res.put("status", false);
                            res.put("msg", "水费核定失败，基础水价设定中未找到相关地块性质和灌溉类型的水价定义！ errCode：003");
                            return res;
                        } else {
                            areaPrice_wiPriceY = wiPriceYS.get(0).getAreaPrice();
                            waterFee = waterFee.add(areaPrice_wiPriceY.multiply(wiLandB.getLandSize()));
                        }
                    }
                    i = insertWiOrgfeeY(year, orgCd, wiLandBSizeSum, volSum, areaPrice_wiPriceY, waterFee);
                }
                if (FeeModeTypeEnum.VOL.getCode().equals(modeType)) {//按水量
                    if (volSum.compareTo(planVolSum) == 1) {//超限用水
                        waterFee = planVolSum.multiply(volPrice_wiPriceY).add((volSum.subtract(planVolSum)).multiply(overPrice_wiFeemodeY));
                    } else {
                        waterFee = volSum.multiply(volPrice_wiPriceY);
                    }
                    i = insertWiOrgfeeY(year, orgCd, wiLandBSizeSum, volSum, volPrice_wiPriceY, waterFee);
                }
                if (i < 1) {
                    res.put("status", false);
                    res.put("msg", "水费核定失败，请联系管理员！ errCode：002");
                    return res;
                }
            }
            if (flag == 3) {
                int i = 0;
                //定义水费
                BigDecimal waterFee = new BigDecimal("0");
                areaPrice_wiOrgpriceY = new BigDecimal("0");
                if (FeeModeTypeEnum.AREA.getCode().equals(modeType)) {//按地块面积 直接地块面积 * 亩均水价
                    //遍历每个地块，根据土地性质和灌溉方式查询对应水价 查不到的，返回提示
                    for (WiLandB wiLandB : wiLandBList) {
                        WiPriceY wiPriceY = new WiPriceY();
                        wiPriceY.setPriceYr(year);
                        wiPriceY.setLandAttr(wiLandB.getLandAttr());
                        wiPriceY.setIrrMode(wiLandB.getIrrMode());
                        WiOrgpriceY wiOrgpriceY = wiOrgpriceYMapper.selectWiOrgpriceYByOrgCdAndYear(year, orgCd);
                        if (wiOrgpriceY == null) {
                            res.put("status", false);
                            res.put("msg", "水费核定失败，特定水价设定中未找到相关地块性质和灌溉类型的水价定义！ errCode：004");
                            return res;
                        } else {
                            areaPrice_wiOrgpriceY = wiOrgpriceY.getAreaPrice();
                            waterFee = waterFee.add(areaPrice_wiOrgpriceY.multiply(wiLandB.getLandSize()));
                        }
                    }
                    i = insertWiOrgfeeY(year, orgCd, wiLandBSizeSum, volSum, areaPrice_wiOrgpriceY, waterFee);
                }
                if (FeeModeTypeEnum.VOL.getCode().equals(modeType)) {//按水量
                    if (volSum.compareTo(planVolSum) == 1) {//超限用水
                        waterFee = planVolSum.multiply(volPrice_wiOrgpriceY).add((volSum.subtract(planVolSum)).multiply(overPrice_wiFeemodeY));
                    } else {
                        waterFee = volSum.multiply(volPrice_wiOrgpriceY);
                    }
                    i = insertWiOrgfeeY(year, orgCd, wiLandBSizeSum, volSum, volPrice_wiOrgpriceY, waterFee);
                }
                if (i < 1) {
                    res.put("status", false);
                    res.put("msg", "水费核定失败，请联系管理员！ errCode：002");
                    return res;
                }
            }
        }
        if (FeeKindEnum.FRAM.getCode().equals(feeKind)) {//按农户
            /**
             * 说明：
             * 查询机构下农户 按农户分组查地块
             * 每个农户核算出水费后落表 多条数据
             * */
            //查询机构下农户
            String[] farmCds = wiOrgfeeYMapper.selectFramCdByOrgCd(orgCd);
            //遍历农户查地块总面积
            BigDecimal wiLandBSizeSum = new BigDecimal("0");
            if (farmCds.length == 0) {
                res.put("status", false);
                res.put("msg", "该机构下没有农户信息！ errCode：005");
                return res;
            }
            BigDecimal waterFee;
            BigDecimal volSum = new BigDecimal("0");
            BigDecimal planVolSum;//限额水量
            if (flag == 1) {
                for (String farmCd : farmCds) {
                    log.info("农户编码：" + farmCd);
                    wiLandBSizeSum = new BigDecimal("0");
                    wiLandBSizeSum = wiLandBSizeSum.add(new BigDecimal(wiOrgfeeYMapper.selectWiLandSize(farmCd) == null ? "0" : wiOrgfeeYMapper.selectWiLandSize(farmCd)));
                    //查水量
                    volSum = volSum.add(new BigDecimal(wiOrgfeeYMapper.getVolSumByFarm(farmCd, startYm, endYm)));
                    //查限额水量
                    planVolSum = new BigDecimal(wiFarmfeeYMapper.getPlanVolSum(farmCd, startYm, endYm));
                    if (FeeModeTypeEnum.AREA.getCode().equals(modeType)) {//按地块面积
                        //算水费
                        waterFee = wiLandBSizeSum.multiply(areaPrice_wiFeemodeY);
                        insertWiFarmY(year, farmCd, wiLandBSizeSum, volSum, areaPrice_wiFeemodeY, waterFee);
                    }
                    if (FeeModeTypeEnum.VOL.getCode().equals(modeType)) {//按水量
                        if (volSum.compareTo(planVolSum) == 1) {//超限用水
                            waterFee = planVolSum.multiply(volPrice_wiFeemodeY).add((volSum.subtract(planVolSum)).multiply(overPrice_wiFeemodeY));
                        } else {
                            waterFee = volSum.multiply(volPrice_wiFeemodeY);
                        }
                        insertWiFarmY(year, farmCd, wiLandBSizeSum, volSum, volPrice_wiFeemodeY, waterFee);
                    }
                }
            }
            if (flag == 2) {
                for (String farmCd : farmCds) {
                    //查农户下的地块 看是地块性质和灌溉方式是否能匹配到定价
                    List<WiLandB> wiLandBList = wiFarmfeeYMapper.selectWiLandListByFarmCd(farmCd);
                    //遍历每个地块，根据土地性质和灌溉方式查询对应水价 查不到的，返回提示
                    for (WiLandB wiLandB : wiLandBList) {
                        WiPriceY wiPriceY = new WiPriceY();
                        wiPriceY.setPriceYr(year);
                        wiPriceY.setLandAttr(wiLandB.getLandAttr());
                        wiPriceY.setIrrMode(wiLandB.getIrrMode());
                        List<WiPriceY> wiPriceYS = wiPriceYMapper.selectWiPriceYList(wiPriceY);
                        if (wiPriceYS == null) {
                            res.put("status", false);
                            res.put("msg", "水费核定失败，年度水价设定中未找到相关地块性质和灌溉类型的水价定义！ errCode：004");
                            return res;
                        }
                    }
                }
                for (String farmCd : farmCds) {
                    wiLandBSizeSum = new BigDecimal("0");
                    waterFee = new BigDecimal("0");
                    //查农户下的地块 看是地块性质和灌溉方式是否能匹配到定价
                    List<WiLandB> wiLandBList = wiFarmfeeYMapper.selectWiLandListByFarmCd(farmCd);
                    //遍历每个地块，根据土地性质和灌溉方式查询对应水价 查不到的，返回提示
                    for (WiLandB wiLandB : wiLandBList) {
                        WiPriceY wiPriceY = new WiPriceY();
                        wiPriceY.setPriceYr(year);
                        wiPriceY.setLandAttr(wiLandB.getLandAttr());
                        wiPriceY.setIrrMode(wiLandB.getIrrMode());
                        List<WiPriceY> wiPriceYS = wiPriceYMapper.selectWiPriceYList(wiPriceY);
                        areaPrice_wiPriceY = wiPriceYS.get(0).getAreaPrice();
                        waterFee = waterFee.add(wiLandB.getLandSize().multiply(areaPrice_wiPriceY));
                    }
                    wiLandBSizeSum = wiLandBSizeSum.add(new BigDecimal(wiOrgfeeYMapper.selectWiLandSize(farmCd) == null ? "0" : wiOrgfeeYMapper.selectWiLandSize(farmCd)));
                    //查水量
                    volSum = volSum.add(new BigDecimal(wiOrgfeeYMapper.getVolSumByFarm(farmCd, startYm, endYm)));
                    //查限额水量
                    planVolSum = new BigDecimal(wiFarmfeeYMapper.getPlanVolSum(farmCd, startYm, endYm));

                    if (FeeModeTypeEnum.AREA.getCode().equals(modeType)) {//按地块面积
                        insertWiFarmY(year, farmCd, wiLandBSizeSum, volSum, areaPrice_wiFeemodeY, waterFee);
                    }
                    if (FeeModeTypeEnum.VOL.getCode().equals(modeType)) {//按水量
                        if (volSum.compareTo(planVolSum) == 1) {//超限用水
                            waterFee = planVolSum.multiply(volPrice_wiPriceY).add((volSum.subtract(planVolSum)).multiply(overPrice_wiFeemodeY));
                        } else {
                            waterFee = volSum.multiply(volPrice_wiPriceY);
                        }
                        insertWiFarmY(year, farmCd, wiLandBSizeSum, volSum, volPrice_wiFeemodeY, waterFee);
                    }
                }
            }
            if (flag == 3) {
                for (String farmCd : farmCds) {
                    //查农户下的地块 看是地块性质和灌溉方式是否能匹配到定价
                    List<WiLandB> wiLandBList = wiFarmfeeYMapper.selectWiLandListByFarmCd(farmCd);
                    //遍历每个地块，根据土地性质和灌溉方式查询对应水价 查不到的，返回提示
                    for (WiLandB wiLandB : wiLandBList) {
                        WiPriceY wiPriceY = new WiPriceY();
                        wiPriceY.setPriceYr(year);
                        wiPriceY.setLandAttr(wiLandB.getLandAttr());
                        wiPriceY.setIrrMode(wiLandB.getIrrMode());
                        WiOrgpriceY wiOrgpriceY = wiOrgpriceYMapper.selectWiOrgpriceYByOrgCdAndYear(year, orgCd);
                        if (wiOrgpriceY == null) {
                            res.put("status", false);
                            res.put("msg", "水费核定失败，特定水价设定中未找到相关地块性质和灌溉类型的水价定义！ errCode：004");
                            return res;
                        }
                    }
                }
                for (String farmCd : farmCds) {
                    wiLandBSizeSum = new BigDecimal("0");
                    waterFee = new BigDecimal("0");
                    //查农户下的地块 看是地块性质和灌溉方式是否能匹配到定价
                    List<WiLandB> wiLandBList = wiFarmfeeYMapper.selectWiLandListByFarmCd(farmCd);
                    //遍历每个地块，根据土地性质和灌溉方式查询对应水价 查不到的，返回提示
                    for (WiLandB wiLandB : wiLandBList) {
                        WiPriceY wiPriceY = new WiPriceY();
                        wiPriceY.setPriceYr(year);
                        wiPriceY.setLandAttr(wiLandB.getLandAttr());
                        wiPriceY.setIrrMode(wiLandB.getIrrMode());
                        WiOrgpriceY wiOrgpriceY = wiOrgpriceYMapper.selectWiOrgpriceYByOrgCdAndYear(year, orgCd);
                        areaPrice_wiOrgpriceY = wiOrgpriceY.getAreaPrice();
                        waterFee = waterFee.add(wiLandB.getLandSize().multiply(areaPrice_wiOrgpriceY));
                    }
                    wiLandBSizeSum = wiLandBSizeSum.add(new BigDecimal(wiOrgfeeYMapper.selectWiLandSize(farmCd) == null ? "0" : wiOrgfeeYMapper.selectWiLandSize(farmCd)));
                    //查水量
                    volSum = volSum.add(new BigDecimal(wiOrgfeeYMapper.getVolSumByFarm(farmCd, startYm, endYm)));
                    //查限额水量
                    planVolSum = new BigDecimal(wiFarmfeeYMapper.getPlanVolSum(farmCd, startYm, endYm));

                    if (FeeModeTypeEnum.AREA.getCode().equals(modeType)) {//按地块面积
                        insertWiFarmY(year, farmCd, wiLandBSizeSum, volSum, areaPrice_wiFeemodeY, waterFee);
                    }
                    if (FeeModeTypeEnum.VOL.getCode().equals(modeType)) {//按水量
                        if (volSum.compareTo(planVolSum) == 1) {//超限用水
                            waterFee = planVolSum.multiply(volPrice_wiOrgpriceY).add((volSum.subtract(planVolSum)).multiply(overPrice_wiFeemodeY));
                        } else {
                            waterFee = volSum.multiply(volPrice_wiOrgpriceY);
                        }
                        insertWiFarmY(year, farmCd, wiLandBSizeSum, volSum, volPrice_wiFeemodeY, waterFee);
                    }
                }
            }
        }
        /**查找地块 并计算水费 end*/
        res.put("status", true);
        res.put("msg", "水费核定完成！");
        return res;
    }

    /**
     * 获取收缴方式
     * @param year
     * @return
     */
    @Override
    public String getFeeKind(String year) {
        WiFeemodeY wiFeemodeY = wiFeemodeYMapper.selectWiFeemodeYByModeYr(year);
        return wiFeemodeY.getFeeKind();
    }

    private int insertWiOrgfeeY(String year, String orgCd, BigDecimal wiLandBSizeSum, BigDecimal volSum, BigDecimal unitPrice, BigDecimal waterFee) {
        //如果存在 删除后添加
        WiOrgfeeY wiOrgfeeY0 = wiOrgfeeYMapper.selectWiOrgfeeYByFeeYrAndOrgCd(year, orgCd);
        if (wiOrgfeeY0 != null) {
            wiOrgfeeYMapper.deleteWiOrgfeeYByFeeYrAndOrgCd(year, orgCd);
        }
        WiOrgfeeY wiOrgfeeY = new WiOrgfeeY();
        wiOrgfeeY.setFeeYr(year);
        wiOrgfeeY.setOrgCd(orgCd);
        wiOrgfeeY.setLandArea(wiLandBSizeSum);
        wiOrgfeeY.setLandVol(volSum);
        wiOrgfeeY.setUnitPrice(unitPrice);
        wiOrgfeeY.setWaterFee(waterFee);
        wiOrgfeeY.setOperId(SecurityUtils.getUserId() + "");
        wiOrgfeeY.setOperTime(new Date());
        return wiOrgfeeYMapper.insertWiOrgfeeY(wiOrgfeeY);
    }

    private int insertWiFarmY(String year, String farmCd, BigDecimal wiLandBSizeSum, BigDecimal volSum, BigDecimal areaPrice_wiFeemodeY, BigDecimal waterFee) {
        //如果存在 删除后添加
        WiFarmfeeY wiFarmfeeY0 = wiFarmfeeYMapper.selectWiFarmfeeYByFeeYrAndFarmCd(year, farmCd);
        if (wiFarmfeeY0 != null) {
            wiFarmfeeYMapper.deleteWiFarmfeeYByFeeYrAndFarmCd(year, farmCd);
        }
        WiFarmfeeY wiFarmfeeY = new WiFarmfeeY();
        wiFarmfeeY.setFeeYr(year);
        wiFarmfeeY.setFarmCd(farmCd);
        wiFarmfeeY.setLandArea(wiLandBSizeSum.setScale(2));
        wiFarmfeeY.setLandVol(volSum.setScale(2));
        wiFarmfeeY.setUnitPrice(areaPrice_wiFeemodeY.setScale(2));
        wiFarmfeeY.setWaterFee(waterFee.setScale(2));
        wiFarmfeeY.setOperId(SecurityUtils.getUserId() + "");
        wiFarmfeeY.setOperTime(new Date());
        log.info(">>>>>>>>>>> "+ wiFarmfeeY);
        return wiFarmfeeYMapper.insertWiFarmfeeY(wiFarmfeeY);
    }

}
